﻿(*                  FnSharp              

   Base class library extensions and frameworks aimed 
   specifically at improving F# developers lives!

   Written by: Huw Simpson
   License: Microsoft Public License (Ms-PL) 
   Available at: http://fnsharp.codeplex.com

*)
namespace FnSharp.Bcl.IO

    open System
    open System.IO

    [<RequireQualifiedAccess>]
    [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
    module File =
        let canRead file =
            try use stream = File.OpenRead (file)
                stream.ReadByte () |> ignore
                true
            with _ -> false

        let waitTillAccessibleAsync (path, timeout) =
            async { let start = DateTime.Now
                    while (DateTime.Now < start + timeout) && canRead (path) = false do
                        do! Async.Sleep (100)
                    return canRead (path) }
                
        let waitTillAccessible (path, timeout) = 
            Async.RunSynchronously (waitTillAccessibleAsync (path, timeout))